package UserUnit;

import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.Serializable;
import javax.media.jai.InterpolationBilinear;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
//This class sends the sample of the original photo before been downloaded it
import javax.swing.ImageIcon;

public class ImpSamplePhoto implements Serializable, SamplePhoto {

    ImageIcon icon = null;
    String imagePath;
    ImageIcon thumbnail = null;

    public void setIcon(String imagePath) {

        PlanarImage im = JAI.create("fileload", imagePath);
        BufferedImage im2 = resize(im, 100, 100);
        this.imagePath = imagePath;
        this.icon = new ImageIcon(im2);
    }

    public ImageIcon getIcon() {
        return icon;
    }

    private static BufferedImage resize(PlanarImage image, float targetWidth, float targetHeight) {
        float scale = 1.0F;

        float s1 = (float) targetWidth / (float) image.getWidth();
        float s2 = (float) targetHeight / (float) image.getHeight();

        if (s1 > s2) {
            scale = s2;
        } else {
            scale = s1;
        }

        InterpolationBilinear interp = new InterpolationBilinear();

        ParameterBlock parameters = new ParameterBlock();

        /*
         * ---- ------ ----- --------
         */
        parameters.addSource(image);
        parameters.add(scale);
        parameters.add(scale);
        parameters.add(0.0F);
        parameters.add(0.0F);
        parameters.add(interp);
        /*
         * --- ------ ----- --------
         */


        PlanarImage temp = JAI.create("scale", parameters);
        return temp.getAsBufferedImage();
    }
}
